/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeDuplicateNodes(ListNode head) {
        Set<Integer> set = new HashSet<>();
        if(head == null) return null;
        ListNode prev = null;
        ListNode cur = head;
        while(cur != null){
            if(set.contains(cur.val)){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                set.add(cur.val);
                prev = cur;
                cur = cur.next;
            }
        }
        return head;
    }
}
